//所有数据
var data=[
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942698',badState:'提交',reportDepartment:'新生儿小婴儿科',reportTime:'2018-04-16',reportType:'首次报告',reportClassify:'新的',reportUnit:'医疗机构'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942699',badState:'上报存档',reportDepartment:'血液科',reportTime:'2018-04-16',reportType:'追踪报告',reportClassify:'一般',reportUnit:'医疗机构'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942699',badState:'上报存档',reportDepartment:'血液科',reportTime:'2018-04-16',reportType:'追踪报告',reportClassify:'一般',reportUnit:'医疗机构'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942699',badState:'终止',reportDepartment:'血液科',reportTime:'2018-04-16',reportType:'首次报告',reportClassify:'一般',reportUnit:'医疗机构'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942699',badState:'上报存档',reportDepartment:'新生儿小婴儿科',reportTime:'2018-04-16',reportType:'首次报告',reportClassify:'一般',reportUnit:'医疗机构'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942699',badState:'上报存档',reportDepartment:'康复科',reportTime:'2018-04-16',reportType:'首次报告',reportClassify:'一般',reportUnit:'医疗机构'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942699',badState:'上报存档',reportDepartment:'康复科',reportTime:'2018-04-16',reportType:'首次报告',reportClassify:'一般',reportUnit:'医疗机构'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942699',badState:'终止',reportDepartment:'康复科',reportTime:'2018-04-16',reportType:'首次报告',reportClassify:'严重',reportUnit:'医疗机构'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942699',badState:'科室审批',reportDepartment:'肾脏免疫科',reportTime:'2018-04-16',reportType:'首次报告',reportClassify:'新的',reportUnit:'医疗机构'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942699',badState:'上报存档',reportDepartment:'肾脏免疫科',reportTime:'2018-04-16',reportType:'首次报告',reportClassify:'新的',reportUnit:'经营企业'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942699',badState:'终止',reportDepartment:'康复科',reportTime:'2018-04-16',reportType:'追踪报告',reportClassify:'严重',reportUnit:'经营企业'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942699',badState:'上报存档',reportDepartment:'新生儿小婴儿科',reportTime:'2018-04-16',reportType:'追踪报告',reportClassify:'新的',reportUnit:'经营企业'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942699',badState:'上报存档',reportDepartment:'新生儿小婴儿科',reportTime:'2018-04-16',reportType:'追踪报告',reportClassify:'新的',reportUnit:'经营企业'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942699',badState:'创建',reportDepartment:'康复科',reportTime:'2019-04-16',reportType:'首次报告',reportClassify:'新的',reportUnit:'经营企业'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942699',badState:'终止',reportDepartment:'血液科',reportTime:'2020-04-16',reportType:'追踪报告',reportClassify:'一般',reportUnit:'经营企业'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942691',badState:'科室审批',reportDepartment:'康复科',reportTime:'2021-04-16',reportType:'追踪报告',reportClassify:'新的',reportUnit:'医疗机构'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942692',badState:'科室审批',reportDepartment:'康复科',reportTime:'2022-04-16',reportType:'首次报告',reportClassify:'新的',reportUnit:'医疗机构'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942693',badState:'终止',reportDepartment:'康复科',reportTime:'2022-05-16',reportType:'首次报告',reportClassify:'新的',reportUnit:'医疗机构'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942695',badState:'终止',reportDepartment:'康复科',reportTime:'2022-06-16',reportType:'追踪报告',reportClassify:'严重',reportUnit:'经营企业'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942692',badState:'科室审批',reportDepartment:'康复科',reportTime:'2022-04-16',reportType:'首次报告',reportClassify:'新的',reportUnit:'医疗机构'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942693',badState:'终止',reportDepartment:'康复科',reportTime:'2022-05-16',reportType:'首次报告',reportClassify:'新的',reportUnit:'医疗机构'},
    {choose:'<input type="checkbox">',badCode:'YWBL20180319102942695',badState:'终止',reportDepartment:'康复科',reportTime:'2022-06-16',reportType:'追踪报告',reportClassify:'严重',reportUnit:'经营企业'},
]

//当前页码
var page = 1;
//每页条数
var count = 8;
//获取tbody
var oTBody = document.querySelector('tbody');
//获取分页容器
var oPagination = document.querySelector('.pagination');
//获取上一页的按钮
var oPrevBtn = document.querySelector('.previous')
//获取下一页的按钮
var oNextBtn = document.querySelector('.next')
//获取第一页按钮
// var oFirstBtn = document.querySelector('firstPage')
//获取修改类型按钮
var oEditBtn=document.querySelector('.list-edit')
//获取发起事件追踪按钮
var oTraceBtn=document.querySelector('.list-trace')
//获取修改事件类型的叉
var editExitBtn = document.querySelector('.edit-Exit')
//获取发起事件追踪的叉
var traceExitBtn = document.querySelector('.trace-edit-Exit')
//获取查询按钮
var oInquireBtn=document.querySelector('.btn-inquire')
//获取清除按钮
var oClearBtn=document.querySelector('.btn-clear')
//获取所有查询框
var inquireInp1=document.querySelector('.condition-number')
var inquireInp2=document.querySelector('.condition-department')
var inquireInp3=document.querySelector('.condition-reportCard')
var inquireInp4=document.querySelector('.condition-reportClassify')
var inquireInp5=document.querySelector('.condition-unitType')
var inquireInp6=document.querySelector('.condition-badState')
var inquireInp7=document.querySelector('.condition-reportTime')
var inquireInp8=document.querySelector('.condition-reportTime1')

//获取不良事件编码
// var oBadCode=document.querySelector('.t-clickInfo')
//获取不良事件表格
var oBadTable=document.querySelector('.t-information')
//获取不良事件表格关闭按钮
var oBadBtn=document.querySelector('.exit-table')


document.onclick=function(){
    if(event.target.className=='t-clickInfo'){
        oBadTable.style.display='block'
    }
}
// document.onclick=function(){
//     if(event.target.className=='exit-table'){
//         oBadTable.style.display='none'
//     }
// }
oBadBtn.onclick=function(){
    oBadTable.style.display='none'
}


/* //不良编码加点击事件
oBadCode.addEventListener('click',appearTableFn)
function appearTableFn(){
    oBadTable.style.display='block'
}
oBadBtn.addEventListener('click',closeBadFn)
function closeBadFn(){
    oBadTable.style.display='none'
} */
//修改类型按钮加事件
oEditBtn.addEventListener('click',openEdit)
function openEdit(){
    document.querySelector('.mask').style.display='flex'
}
//修改发起事件追踪加事件
oTraceBtn.addEventListener('click',openTrace)
function openTrace(){
    document.querySelector('.trace-mask').style.display='flex'
}
//退出修改类型
editExitBtn.addEventListener('click',exitEdit)
function exitEdit(){
    document.querySelector('.mask').style.display='none'
}
//退出发起事件追踪
traceExitBtn.addEventListener('click',exitTrace)
function exitTrace(){
    document.querySelector('.trace-mask').style.display='none'
}


//页面初始化的渲染功能
function render() {
    // 查看所有查询框的内容
    var str1 = inquireInp1.value;//获取查询内容1
    var str2 = inquireInp2.value;//获取查询内容2
    var str3 = inquireInp3.value;//获取查询内容3
    var str4 = inquireInp4.value;//获取查询内容4
    var str5 = inquireInp5.value;//获取查询内容5
    var str6 = inquireInp6.value;//获取查询内容6
    var str7 = inquireInp7.value==''?new Date('2000-8-25').getTime():new Date(inquireInp7.value).getTime();//获取查询内容7
    var str8 = inquireInp8.value==''?new Date().getTime():new Date(inquireInp8.value).getTime();//获取查询内容8
    var inquireData = [];//搜索后的数据
    var data1=[];
    var data2=[];
    var data3=[];
    var data4=[];
    var data5=[];
    var data6=[];
    // var data7=[];

    if(str1.trim()==''){
        data1=data;
    }else {
        data1=data.filter(function(v){
            return v.badCode==str1
        })
    }
    console.log(data,data1);

    if(str2=='---请选择---'){
        data2=data1;
    }else {
        data2=data1.filter(function(v){
            return v.reportDepartment==str2
        })
    }
    console.log(data1,data2);

    if(str3=='---请选择---'){
        data3=data2;
    }else {
        data3=data2.filter(function(v){
            return v.reportType==str3
        })
    }
    console.log(data2,data3);

    if(str4=='---请选择---'){
        data4=data3;
    }else {
        data4=data3.filter(function(v){
            return v.reportClassify==str4
        })
    }
    console.log(data3,data4);

    if(str5=='---请选择---'){
        data5=data4;
    }else {
        data5=data4.filter(function(v){
            return v.reportUnit==str5
        })
    }
    console.log(data4,data5);

    if(str6=='---请选择---'){
        data6=data5;
    }else {
        data6=data5.filter(function(v){
            return v.badState==str6
        })
    }
    console.log(data5,data6);

    inquireData=data6.filter(function(v){
        var time=new Date(v.reportTime).getTime();
        return time>=str7 && time<=str8
    })
    console.log(data6,inquireData);

    /* if(str7==''){
        inquireData=data6;
    }else {
        inquireData=data6.filter(function(v){
            return v.reportTime.includes(str7)
        })
    }
    console.log(data6,inquireData); */


//让table为空
    oTBody.innerHTML = '';
//根据count和page准备要渲染的数据
    var renderArr = inquireData.slice((page - 1) * count, page * count);
//循环要渲染的数据
    for (var i = 0; i < renderArr.length; i++) {
    //创建tr
        var oTr = document.createElement('tr');
    //给tr创建内容
        oTr.innerHTML = `
        <td>${renderArr[i].choose}</td>
        <td><a class="t-clickInfo">${renderArr[i].badCode}</a></td>
        <td>${renderArr[i].badState}</td>
        <td>${renderArr[i].reportDepartment}</td>
        <td>${renderArr[i].reportTime}</td>
        <td>${renderArr[i].reportType}</td>
        <td>${renderArr[i].reportClassify}</td>
        <td>${renderArr[i].reportUnit}</td>
        `;
    //把tr创建到tbody里面
        oTBody.appendChild(oTr);
    }

    //渲染页码
    //找所有页码
    var aPageBtn = document.querySelectorAll('.pageBtn');

    //删除所有页码
    for (var i = 0; i < aPageBtn.length; i++) {
        oPagination.removeChild(aPageBtn[i].parentNode);
    }

    //根据数据的长度重新创建页码
    for (var i = 1; i <= Math.ceil(inquireData.length / count); i++) {
        //创建li
        var oLi = document.createElement('li');
        //给li创建内容
        oLi.innerHTML = `<a class="pageBtn" href="#">${i}</a>`;
        //如果li和当前的页码一致，加一个选中的class
        if (i == page) {
            oLi.classList.add('active');
        }
        //在下一页的前面插入页码
        oPagination.insertBefore(oLi, oPagination.lastElementChild);
    }
}

//查询
oInquireBtn.addEventListener('click', inquireFn);
function inquireFn() {
    page = 1;
    render();
}
//清除
oClearBtn.addEventListener('click', clearFn);
function clearFn() {
    inquireInp1.value='';
    inquireInp2.value='---请选择---';
    inquireInp3.value='---请选择---';
    inquireInp4.value='---请选择---';
    inquireInp5.value='---请选择---';
    inquireInp6.value='---请选择---';
    inquireInp7.value='';
    page=1;
    render()
}


//一上来就渲染一次数据
render();
//给分页加事件
oPagination.addEventListener('click', changePage);
function changePage() {
    //如果当前点击的按钮有pageBtn这个class名字
    if (event.target.className == 'pageBtn') {
        //修改全局的page
        page = event.target.innerText;
        //渲染table，渲染分页
        render();
    }
}
//给上一页/下一页加事件
oPrevBtn.addEventListener('click', prev);
oNextBtn.addEventListener('click', next);
function prev() {
    if (page > 1) {
        page--;
        render();
    }
}
function next() {
    if (page < Math.ceil(data.length / count)) {
        page++;
        render();
    }
}